import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadDemo18 {
    public static void main(String[] args) {
        //基于阻塞队列 写一个消费者生产者
        // 上述代码, 让生产者, 每隔 1s 生产一个元素.
        // 让消费者则直接消费, 不受限制.
        BlockingQueue<Integer> blockingQueue =new LinkedBlockingQueue<>();
        //消费者
        Thread t1=new Thread( ()->{
           while(true)
           {
               int value= 0;
               try {
                   value = blockingQueue.take();
                   System.out.println("消费者是:"+ value);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }

           }
        });
        t1.start();
        //生产者
        Thread t2=new Thread( ()-> {
            int value=0;
            while(true)
            {
                try {
                    System.out.println("生产者是:"+value);
                    blockingQueue.put(value);
                    value++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        t2.start();
    }
}
